/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Telas;

import Gerenciar.GerenciarServico;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.table.DefaultTableModel;
import entidades.Servicos;
import java.awt.event.KeyEvent;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Deyves
 */
public class CadastroServico extends javax.swing.JInternalFrame {

    int id = -1;    //--------------- ATRIBUTO PARA PEGAR O ID SELECIONADO NA TABELA E PASSAR PARA UPDATE OU EXCLUSÃO
    boolean alterar = false;   ///----------- CONDIÇÃO PARA ENTRAR EM UPDATE
    DecimalFormat df = new DecimalFormat("#,##0.00;-#,##0.00");

    public CadastroServico() {
        initComponents();
        atualizarTabelaServico();
    }
    private static CadastroServico instance = null;

    public static CadastroServico getInstance() {  ////////----------------- INICIA UMA NOVA INSTANCIA DA TELA
        if (instance == null) {
            instance = new CadastroServico();

        }
        return instance;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu2 = new javax.swing.JPopupMenu();
        jLabel1 = new javax.swing.JLabel();
        txtNomeServico = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btnCadastrar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblServicos = new javax.swing.JTable();
        btnAlterar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        txtValorServico = new javax.swing.JFormattedTextField();
        btnCancelar = new javax.swing.JButton();
        btnLimpar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setTitle("CADASTRO DE SERVIÇOS");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/catalog png.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Gadugi", 0, 16)); // NOI18N
        jLabel1.setText("Nome do Serviço:");

        txtNomeServico.setToolTipText("Nome do Serviço");
        txtNomeServico.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtNomeServicoFocusLost(evt);
            }
        });
        txtNomeServico.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtNomeServicoKeyPressed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Gadugi", 0, 16)); // NOI18N
        jLabel2.setText("Valor R$:");

        btnCadastrar.setFont(new java.awt.Font("Gadugi", 0, 14)); // NOI18N
        btnCadastrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/db_add.png"))); // NOI18N
        btnCadastrar.setText("Cadastrar");
        btnCadastrar.setToolTipText("Cadastrar um Serviço");
        btnCadastrar.setBorderPainted(false);
        btnCadastrar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btnCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCadastrarActionPerformed(evt);
            }
        });

        tblServicos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "Serviço", "Valor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblServicos.setFocusable(false);
        tblServicos.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tblServicosKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tblServicosKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(tblServicos);
        tblServicos.getColumnModel().getColumn(0).setMinWidth(0);
        tblServicos.getColumnModel().getColumn(0).setMaxWidth(0);
        tblServicos.getColumnModel().getColumn(1).setMinWidth(300);
        tblServicos.getColumnModel().getColumn(1).setMaxWidth(300);
        tblServicos.getColumnModel().getColumn(2).setResizable(false);

        btnAlterar.setFont(new java.awt.Font("Gadugi", 0, 14)); // NOI18N
        btnAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/edit.png"))); // NOI18N
        btnAlterar.setText("Alterar");
        btnAlterar.setToolTipText("Alterar um Serviço");
        btnAlterar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        btnExcluir.setFont(new java.awt.Font("Gadugi", 0, 14)); // NOI18N
        btnExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/db_remove.png"))); // NOI18N
        btnExcluir.setText("Excluir");
        btnExcluir.setToolTipText("Excluir um Serviço");
        btnExcluir.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });
        btnExcluir.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnExcluirKeyPressed(evt);
            }
        });

        txtValorServico.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00; -#,##0.00"))));
        txtValorServico.setText("0,00");
        txtValorServico.setToolTipText("Valor do Serviço");
        txtValorServico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtValorServicoActionPerformed(evt);
            }
        });
        txtValorServico.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtValorServicoFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtValorServicoFocusLost(evt);
            }
        });
        txtValorServico.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtValorServicoKeyPressed(evt);
            }
        });

        btnCancelar.setFont(new java.awt.Font("Gadugi", 0, 14)); // NOI18N
        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/cancel.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.setToolTipText("Cancelar Operação");
        btnCancelar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        btnCancelar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnCancelarKeyPressed(evt);
            }
        });

        btnLimpar.setFont(new java.awt.Font("Gadugi", 0, 14)); // NOI18N
        btnLimpar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/eraser 2.png"))); // NOI18N
        btnLimpar.setText("Limpar");
        btnLimpar.setToolTipText("Limpar os Campos");
        btnLimpar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btnLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimparActionPerformed(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/sideral menor.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 382, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btnCadastrar)
                    .addComponent(btnExcluir)
                    .addComponent(btnAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelar)))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNomeServico, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtValorServico, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnLimpar)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnAlterar, btnCadastrar, btnCancelar, btnExcluir});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtNomeServico, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtValorServico, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2)
                                .addComponent(btnLimpar))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnCadastrar)
                        .addGap(18, 18, 18)
                        .addComponent(btnAlterar)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar)
                        .addGap(18, 18, 18)
                        .addComponent(btnExcluir)
                        .addContainerGap(188, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addGap(1, 1, 1))))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {txtNomeServico, txtValorServico});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnAlterar, btnCadastrar, btnCancelar, btnExcluir});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCadastrarActionPerformed
///--------------------BOTAO DE CADASTRO DE NOVO SERVIÇO

        Servicos servicos = new Servicos();  ////----------- INSTANCIA UM NOVO OBJETO DO TIPO SERVIÇO
        if (alterar == true) { ////////////////-------- CONDIÇÃO EM QUE SE O ALTERAR FOR TRUE, ENTAO PASSA O OBJETO COM ID PREENCHIDO
            servicos.setDescricao(txtNomeServico.getText());
            txtValorServico.setText(txtValorServico.getText().replace(".", ""));
            servicos.setPreco(Double.parseDouble(txtValorServico.getText().replace(",", ".")));
            servicos.setId(id);

            if (GerenciarServico.gravar(servicos)) {    //--------- CONDIÇÃO PARA CASO ENVIAR O OBJETO E SALVAR NO BANCO
                limparCampos();
                limparTabela();
                atualizarTabelaServico();
                JOptionPane.showMessageDialog(null, "Serviço alterado com sucesso!");
                txtNomeServico.requestFocus();
                return;
            }

        } else {
            ///----------- CONDIÇÃO EM QUE SE O ALTERAR NAO FOR TRUE, ENTÃO É UM NOVO SERVIÇO E ENTRA AQUI

            if (!txtNomeServico.getText().equals("") || !txtValorServico.getText().equals("0,00") && alterar == false) {
                servicos.setDescricao(txtNomeServico.getText());
                txtValorServico.setText(txtValorServico.getText().replace(".", ""));
                servicos.setPreco(Double.parseDouble(txtValorServico.getText().replace(",", ".")));

                if (GerenciarServico.gravar(servicos)) {        //--------- CONDIÇÃO PARA CASO ENVIAR O OBJETO E SALVAR NO BANCO
                    limparCampos();
                    limparTabela();
                    atualizarTabelaServico();
                    JOptionPane.showMessageDialog(null, "Serviço inserido com sucesso!");
                    txtNomeServico.requestFocus();
                    return;
                }
            }
        }
        JOptionPane.showMessageDialog(null, "Preencha todos os campos corretamente!", "Campos Incorretos", 2);

    }//GEN-LAST:event_btnCadastrarActionPerformed
    private void txtValorServicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtValorServicoActionPerformed
//        BigDecimal valor = new BigDecimal(txtValorServico.getText());
        //--------------- FORMATAÇÃO DO CAMPO DE VALOR
        NumberFormat nf = NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));
        Double valor;
        //valor = nf.parse(txtValorServico.getText()).doubleValue();
        try {
            valor = df.parse(txtValorServico.getText()).doubleValue();
            txtValorServico.setText(valor.toString());
//            Double formatado = nf.parse(valor).doubleValue();
            // formatado = formatado.replace("R$", "");
        } catch (ParseException ex) {
            Logger.getLogger(CadastroServico.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_txtValorServicoActionPerformed

    private void txtValorServicoFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtValorServicoFocusGained
        //--------------- FORMATAÇÃO DO CAMPO DE VALOR
        if (txtValorServico.getText().equals("0,00")) {
            txtValorServico.setText("");
        }
    }//GEN-LAST:event_txtValorServicoFocusGained

    private void txtValorServicoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtValorServicoKeyPressed
        //---------------- QUANDO USUARIO APERTAR O TAB, VAI PARA BOTÃO CADASTRAR
        if (evt.getKeyCode() == KeyEvent.VK_TAB) {
            btnCadastrar.requestFocus();
        }
    }//GEN-LAST:event_txtValorServicoKeyPressed

    private void txtValorServicoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtValorServicoFocusLost
        //--------------- FORMATAÇÃO DO CAMPO DE VALOR

        if (txtValorServico.getText().trim().equals("")) {
            txtValorServico.setText("0,00");
        }
//        Double valor;
//        try {
//            valor = df.parse(txtValorServico.getText()).doubleValue();
//            txtValorServico.setText(valor.toString());
//        } catch (ParseException ex) {
//            Logger.getLogger(CadastroServico.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }//GEN-LAST:event_txtValorServicoFocusLost

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        if (tblServicos.getSelectedRow() >= 0) {  //----------- CONDIÇÃO EM QUE SE TIVER UMA LINHA SELECIONA, ENTAO EXECUTA 
            Servicos servicos = new Servicos();
            txtNomeServico.setText(tblServicos.getValueAt(tblServicos.getSelectedRow(), 1).toString());
            txtValorServico.setText("");
            txtValorServico.setText(tblServicos.getValueAt(tblServicos.getSelectedRow(), 2).toString().replace(".", ","));
            id = (int) tblServicos.getValueAt(tblServicos.getSelectedRow(), 0);
            tblServicos.clearSelection();
            alterar = true;
            Double valor;
            try {
                valor = df.parse(txtValorServico.getText()).doubleValue();
                txtValorServico.setText(valor.toString());
            } catch (ParseException ex) {
                Logger.getLogger(CadastroServico.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_btnAlterarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        //------------   SE CANCELAR, RESTAURA TODOS VALORES DEFAULT E FECHA INSTANCIA DA TELA
        limparCampos();
        instance.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimparActionPerformed
        limparCampos();
    }//GEN-LAST:event_btnLimparActionPerformed

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        limparCampos();
    }//GEN-LAST:event_formInternalFrameClosing

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        excluirServico();
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void txtNomeServicoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNomeServicoFocusLost
        txtNomeServico.setText(txtNomeServico.getText().toUpperCase());
    }//GEN-LAST:event_txtNomeServicoFocusLost

    private void tblServicosKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblServicosKeyPressed
    }//GEN-LAST:event_tblServicosKeyPressed

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
    }//GEN-LAST:event_formKeyPressed

    private void btnCancelarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnCancelarKeyPressed
    }//GEN-LAST:event_btnCancelarKeyPressed

    private void btnExcluirKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnExcluirKeyPressed
    }//GEN-LAST:event_btnExcluirKeyPressed

    private void tblServicosKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblServicosKeyTyped
    }//GEN-LAST:event_tblServicosKeyTyped

    private void txtNomeServicoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNomeServicoKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
            excluirServico();
        }
    }//GEN-LAST:event_txtNomeServicoKeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnCadastrar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnLimpar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPopupMenu jPopupMenu2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblServicos;
    private javax.swing.JTextField txtNomeServico;
    private javax.swing.JFormattedTextField txtValorServico;
    // End of variables declaration//GEN-END:variables

    private void limparCampos() { //--------   MÉTODO PARA LIMPAR CAMPOS
        txtNomeServico.setText("");
        txtValorServico.setText("0,00");
        tblServicos.clearSelection();
        alterar = false;
        id = -1;
    }

    private void atualizarTabelaServico() { //--------- METODO DE ATUALIZAR TABELA
        limparTabela();
        List<Servicos> listaServicos = GerenciarServico.trazerServicos();
        for (Servicos servicos : listaServicos) {
            Object[] obj = new Object[3];
            obj[0] = servicos.getId();
            obj[1] = servicos.getDescricao();
            obj[2] = servicos.getPreco();
            ((DefaultTableModel) tblServicos.getModel()).addRow(obj);
        }
    }

    private void limparTabela() { //---------- LIMPAR TABELA
        int numLinhas = ((DefaultTableModel) tblServicos.getModel()).getRowCount();
        for (int i = numLinhas - 1; i >= 0; i--) {
            ((DefaultTableModel) tblServicos.getModel()).removeRow(i);
        }
    }

    private void excluirServico() {

        //-------------------------PARTE DE CONFIRMAÇÃO PARA EXCLUIR UM SERVIÇO
        //-------------------------------------------------------------------------------------------


        if (tblServicos.getSelectedRow() >= 0) {
            if (JOptionPane.showConfirmDialog(null, "Atenção!! \n Você está prestes a excluir um serviço!!", "ATENÇÃO", 2, 0) == JOptionPane.YES_OPTION) {
                Servicos servicos = new Servicos();
                servicos.setDescricao("");
                txtValorServico.setText("");
                servicos.setId((int) tblServicos.getValueAt(tblServicos.getSelectedRow(), 0));
                GerenciarServico.excluir(servicos);
                limparCampos();
                limparTabela();
                atualizarTabelaServico();
                JOptionPane.showMessageDialog(null, "Serviço excluido com sucesso!", "Exclusão", 1);
            }

        } else {
            JOptionPane.showMessageDialog(null, "Selecione algum serviço para excluir!", "Atençao", 2);
        }
    }
}
